import requests
from bs4 import BeautifulSoup
import pymysql

def get_wenyin_booklist():
    # 通过添加头部模拟浏览器访问，模拟一个正常访问者
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36'}
    url = r'http://book.duyima.com/web/libraryshow/more?keywords=&code=mWksP80d&type=1&limit=24'

    for page_num in range(1, 58):
        url_page = url + '&page=' + str(page_num)
        html_doc = requests.get(url_page, headers=headers, timeout=60)
        html_doc.encoding = 'UTF-8'
        # 从网页中找到名字和图片地址
        soup = BeautifulSoup(html_doc.text, 'html.parser')
        page_book = soup.find_all(name='img', attrs={"class": "book-img"})
        for book_item in page_book:
            booktitle=book_item.get('alt')
            bookimg=book_item.get('src')
            # print(booktitle, bookimg)
            try:
                conn = pymysql.connect(
                    host='localhost', user='root', password="admin",
                    database='python', port=3306,
                    charset='utf8'
                )
                cursor = conn.cursor()
                sql_insert = "insert into cmdb_book(id,bookname,pic) values(null,%s,%s)"
                cursor.execute(sql_insert, (booktitle, bookimg))
            except Exception as e:
                print(e)
                conn.rollback()
            finally:
                cursor.close()
                conn.close()


if __name__ == "__main__":
    get_wenyin_booklist()